#ifndef _INCLUDE_RING_BUFFER_H_
#define _INCLUDE_RING_BUFFER_H_


typedef struct {
	unsigned char 	*buffer;
	int				length;
	int				start;
	int				end;
}ring_buffer_t;


void ring_buffer_init(ring_buffer_t *buffer, int length);
void ring_buffer_destroy(ring_buffer_t *buffer);
int ring_buffer_read(ring_buffer_t *buffer, char *dest, int amount);
int ring_buffer_write(ring_buffer_t *buffer, const char *src, int length);


#define ring_buffer_available_data(buff)	(((buff)->end + 1) % (buff)->length - (buff)->start - 1)
#define ring_buffer_available_space(buff)	((buff)->length - (buff)->end - 1)
#define ring_buffer_is_full(buff)			(ring_buffer_available_data(buff) - (buff)->length == 0)
#define ring_buffer_is_empty(buff)			(ring_buffer_available_data(buff) == 0)
#define ring_buffer_starts_at(buff)			((buff)->buffer + (buff)->start)
#define ring_buffer_ends_at(buff)			((buff)->buffer + (buff)->end)
#define ring_buffer_commit_read(buff, amount)	((buff)->start = ((buff)->start + (amount)) % (buff)->length)
#define ring_buffer_commit_write(buff, amount)	((buff)->end = ((buff)->end + (amount)) % (buff)->length)





#endif

